<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\AdminService;
use Illuminate\Http\Request;

class AdminController extends BaseController
{
    //管理员

    public function __construct()
    {
    }

    //列表
    public function getIndex()
    {
        $res = (new AdminService())->getIndex();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //一个
    public function getOne(Request $request)
    {
        $id = $request->post('id');
        $res = (new AdminService())->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //保存
    public function save(Request $request)
    {
        $id = $request->post('id', 0);
        $title = $request->post('title');
        if (!$title) {
            return $this->toJson(-2, '管理员名称必须的');
        }
        $res = (new AdminService())->update($id, $title);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //更新密码
    public function setPwd(Request $request)
    {
        //dd(md5('123456'));
        $id = $request->post('id', 0);
        $pwd = $request->post('pwd');
        $pwd2 = $request->post('pwd2');
        if (!$pwd || !$pwd2) {
            return $this->toJson(-2, '密码须的');
        }
        $res = (new AdminService())->setPwd($id, $pwd, $pwd2);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}